package com.zsy.petshop_01.config;

import com.github.xiaoymin.knife4j.core.extend.OpenApiExtendSetting;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@Import(OpenApiExtensionResolver.class)
public class SwaggerConfig {

    private final OpenApiExtensionResolver openApiExtensionResolver;

    public SwaggerConfig(@Lazy OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean
    public OpenApiExtendSetting openApiExtendSetting() {
        return new OpenApiExtendSetting();
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zsy.petshop_01"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .extensions(openApiExtensionResolver.buildExtensions("分组名称"));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Pet-Shop swagger文档")
                .description("宠物店管理系统")
                .version("1.0.0")
                .build();
    }
}